var openidXcx='';
var unionid='';
var deivceid = '';
const Paho = require('../../utils/paho-mqtt.js')
let client = null;
var ts; // 计时器
var cxsjs= 90;
var hgsjs = 180;
var timer=0;
var online = 0;
var shar;
//计时
function Countdowns(that) {
  var js = that.data.js;
  var tsss = setTimeout(function () {
    if (timer<=0){
      clearTimeout(tsss);
    }else{
      timer--;
      console.log(timer);
      that.setData({
        js:timer
      })
    }
    Countdowns(that);
  }, 1000);
}
// 倒计时
function Countdown(that){
  var cxsj = that.data.cxsj;
  var hgsj = that.data.hgsj;
  ts = setTimeout(function () {

    if (hgsjs <= 1) {
      that.setData({
        hgsj: true
      });
      clearTimeout(ts);
    } else {
      if (cxsjs <= 1) {
        that.setData({
          cxsj:true
        });
        hgsjs--;
      } else {
        cxsjs--;
      }
    }
    console.log(cxsjs + "----Countdown----" + hgsjs);
    Countdown(that);
  }, 1000);
}
Page({
  data: {
    logs: [],
    //选中的订单的设备号
    deviceId:'',
    //选中的订单的设备所在酒店编号
    hotelId: '',
    //选中的模式类型1.理疗模式2.快速冲洗4.记忆模式5.遥控器模式
    types:-1,
    //选中的模式的名称，用于模式介绍
    modelName:'',
    //选中的模式描述，用于模式介绍
    modelExplain: '',
    //选中模式的图片，用于模式介绍
    modelUrl: '',
    //订单信息
    orderList: [],
    //用户是第一次使用false.否(查看全部模式)true.是
    initUser: false,
    //理疗模式type=1
    liliaoList: [],
    //快速冲洗模式type=2
    peedList: [],
    //记忆模式type=4
    memoryList:[],
    //全部模式
    allList: [],
    //理疗模式弹出框type=1
    patternLiLiao:false,
    //快速冲洗模式弹出框type=2
    patternPeed: false,
    //记忆模式弹出框type=4
    patternUser: false,
    //遥控器模式弹出框type=5
    patternControl: false,
    //生产记忆模式弹出框
    showMemory: false,
    //喷杆位置
    sprayHead:0,
    //水势
    waterPotential:1,
    //水温
    waterTemperature:1,
    //烘干风温
    dryingTemperature:1,
    //清洗方式
    method:1,
    //坐温
    sitTemperature:1,
    //冲洗时间
    flushTime:0,
    //烘干时间
    dryingTime:0,
    //总时长
    patternTime:0,
    //sterilize:false,
    /** 生产记忆模式的input输入 */
    inputModel: "",
    //生产记忆的模式类型
    memoryType:-1,
    //滑块视图变量值
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    circular: false,
    interval: 2000,
    duration: 500,
    previousMargin: 0,
    nextMargin: 0,
    //资讯
    zxlist: [],
    //商品
    splists:[],
    //冲洗时间
    cxsj:false,
    //烘干时间
    hgsj:false,
    //jis
    js:0,
    //跳转外部链接地址
    skipurl:'',
    mesKey:'',
    zc:'',
    jq:'',
    bm:'',
    am:'',
    ns:'',
    nvs:'',
    lr:'',
    et:'',
    zdy1:'',
    zdy2:'',
    zdy3:'',
    cxss1:'',
    cxss2: '',
    cxss3: '',
    cxsw1: '',
    cxsw2: '',
    cxsw3: '',
    zw1:'',
    zw2: '',
    zw3: '',
    hgdw1: '',
    hgdw2: '',
    hgdw3: '',
    cxms1:'',
    cxms2:'',
    cxms3:'',
    wgd1:'',
    wgd:1


  },

  //mqtt方法
  init: function () {
    /**
     * IoT 初始化
     */
    var timestamp = Date.parse(new Date());
    client = new Paho.Client('ccf5f43883f344ed92a1e5f3f2acae8b.mqtt.iot.bj.baidubce.com', 8884, "admin" + timestamp);

    client.connect({
      userName: "ccf5f43883f344ed92a1e5f3f2acae8b/admin",
      password: "P66oRBOysrISxIVGONVAkF73EYM0+s/ldfhEWxZRVsI=",
      useSSL: true,
      keepAliveInterval: 60,
      onSuccess: function () {
        client.subscribe("$baidu/iot/general/"+deivceid+"/#", {
          qos: 1
        });
        client.subscribe("$baidu/sys/event/disconnect", {
          qos: 1
        });
        client.subscribe("$baidu/sys/event/connect", {
          qos: 1
        });
      }
    });
  },
  msg_callback: function () {
    let that = this;
    /**
     * IoT 消息回调
     */
    client.onMessageArrived = function (msg) {
      var top = msg.topic.indexOf("/in");
      if (top > 0) {
        var mes = msg.payloadString;
        var cjkz = mes.indexOf("cjkz");
        var cjtz = mes.indexOf("cjtz");
        if (cjtz > 0) {
          clearTimeout(ts);
          cxsjs = 90;
          hgsjs = 180;
          that.setData({
            patternControl: false,
            patternLiLiao: false,
            patternUser: false,
            patternPeed: false,
            cxsj: false,
            hgsj: false
          })
        }
        if (cjkz > 0) {
          clearTimeout(ts);
          Countdown(that);
          var val = mes.split(":")[1].replace("\'", "").replace("'}", "");
          val = parseFloat(val);
          if (val == 1) {
            that.setData({
              patternLiLiao: true,
              patternPeed: false,
              patternUser: false,
              patternControl: false,
              zc: 2,
              jq: '',
              bm: '',
              am: '',
              ns: '',
              nvs: '',
              lr: '',
              et: '',
              zdy1: '',
              zdy2: '',
              zdy3: ''
            })
          }
          if (val == 2) {
            that.setData({
              patternLiLiao: true,
              patternPeed: false,
              patternUser: false,
              patternControl: false,
              zc: '',
              jq: 2,
              bm: '',
              am: '',
              ns: '',
              nvs: '',
              lr: '',
              et: '',
              zdy1: '',
              zdy2: '',
              zdy3: ''
            })
          }
          if (val == 3) {
            that.setData({
              patternLiLiao: true,
              patternPeed: false,
              patternUser: false,
              patternControl: false,
              zc: '',
              jq: '',
              bm: 2,
              am: '',
              ns: '',
              nvs: '',
              lr: '',
              et: '',
              zdy1: '',
              zdy2: '',
              zdy3: ''
            })
          }
          if (val == 4) {
            that.setData({
              patternLiLiao: true,
              patternPeed: false,
              patternUser: false,
              patternControl: false,
              zc: '',
              jq: '',
              bm: '',
              am: 2,
              ns: '',
              nvs: '',
              lr: '',
              et: '',
              zdy1: '',
              zdy2: '',
              zdy3: ''
            })
          }
          if (val == 5) {
            that.setData({
              patternUser: true,
              patternLiLiao: false,
              patternPeed: false,
              patternControl: false,
              zc: '',
              jq: '',
              bm: '',
              am: '',
              ns: 2,
              nvs: '',
              lr: '',
              et: '',
              zdy1: '',
              zdy2: '',
              zdy3: ''

            })
          }
          if (val == 6) {
            that.setData({
              patternUser: true,
              patternLiLiao: false,
              patternPeed: false,
              patternControl: false,
              zc: '',
              jq: '',
              bm: '',
              am: '',
              ns: '',
              nvs: 2,
              lr: '',
              et: '',
              zdy1: '',
              zdy2: '',
              zdy3: ''
            })
          }
          if (val == 7) {
            that.setData({
              patternUser: true,
              patternLiLiao: false,
              patternPeed: false,
              patternControl: false,
              zc: '',
              jq: '',
              bm: '',
              am: '',
              ns: '',
              nvs: '',
              lr: 2,
              et: '',
              zdy1: '',
              zdy2: '',
              zdy3: ''
            })
          }
          if (val == 8) {
            that.setData({
              patternUser: true,
              patternLiLiao: false,
              patternPeed: false,
              patternControl: false,
              zc: '',
              jq: '',
              bm: '',
              am: '',
              ns: '',
              nvs: '',
              lr: '',
              et: 2,
              zdy1: '',
              zdy2: '',
              zdy3: ''
            })
          }
        }
      }
      console.log(msg.topic, msg.payloadString);
      var stjson =msg.payloadString;
      var jsonStr = stjson.replace(/'/g, '"');
      if (typeof jsonStr != 'object') {
        jsonStr = jsonStr.replace(/\ufeff/g, "");//重点
        var jj = JSON.parse(jsonStr);
        if(jj.wgd){
          var ts = '';
          if(jj.wgd==1){
            ts="btnss";
          }else{
            ts="";
          }
          that.setData({
            wgd1: ts,
            wgd:jj.wgd
          })
        }
        if(jj.cxss){
          if(jj.cxss==1){
            that.setData({
              cxss1:"btnss",
              cxss2:"",
              cxss3:'' 
            })
          }
          if(jj.cxss==2){
            that.setData({
              cxss1:'',
              cxss2: "btnss",
              cxss3: ''
            })
          }
          if(jj.cxss==3){
            that.setData({
              cxss1: '',
              cxss2: '',
              cxss3:"btnss"
            })
          }
        }
        if (jj.cxsw) {
          if (jj.cxsw == 1) {
            that.setData({
             cxsw1: "btnss",
              cxsw2: "",
              cxsw3: ''
            })
          }
          if (jj.cxsw == 2) {
            that.setData({
              cxsw1: '',
              cxsw2: "btnss",
              cxsw3: ''
            })
          }
          if (jj.cxsw == 3) {
            that.setData({
              cxsw1: '',
              cxsw2: '',
              cxsw3: "btnss"
            })
          }
        }
        
        if (jj.cxms) {
          if (jj.cxms == 1) {
            that.setData({
              cxms1: "btnss",
              cxms2: "",
             cxms3: ''
            })
          }
          if (jj.cxms == 2) {
            that.setData({
              cxms1: '',
              cxms2: "btnss",
              cxms3: ''
            })
          }
          if (jj.cxms == 3) {
            that.setData({
              cxms1: '',
              cxms2: '',
             cxms3: "btnss"
            })
          }
        }
        if (jj.zw) {
          if (jj.zw == 1) {
            that.setData({
             zw1: "btnss",
             zw2: "",
              zw3: ''
            })
          }
          if (jj.zw == 2) {
            that.setData({
             zw1: '',
              zw2: "btnss",
              zw3: ''
            })
          }
          if (jj.zw == 3) {
            that.setData({
             zw1: '',
              zw2: '',
              zw3: "btnss"
            })
          }
        }
        if (jj.hgdw) {
          if (jj.hgdw == 1) {
            that.setData({
              hgdw1: "btnss",
              hgdw2: "",
              hgdw3: ''
            })
          }
          if (jj.hgdw == 2) {
            that.setData({
              hgdw1: '',
              hgdw2: "btnss",
              hgdw3: ''
            })
          }
          if (jj.hgdw == 3) {
            that.setData({
              hgdw1: '',
              hgdw2: '',
              hgdw3: "btnss"
            })
          }
        }
        var devicename = jj.deviceName;
        console.log(devicename);
        if (devicename == deivceid) {
          if (jj.connectionType == "CONNECT") {
            online = 1;
            wx.showToast({
              title: "设备已联网",
              icon: 'none',
              duration: 1000
            })
          }
          if (jj.connectionType == "DISCONNECT") {
            wx.showToast({
              title: "设备断网",
              icon: 'none',
              duration: 1000
            })
            online = 0;
          }
        }
      }
    }
  },
  publish_msg: function (topic, msg) {
    /**
     * IoT 发送消息
     */
    var message = new Paho.Message(msg);
    message.destinationName = topic;
    message.qos = 1;
    client.send(message);
  },
  formSubmit: function (e) {
    let msg = e.detail.value.msg;
    this.publish_msg("okf/msg/test", msg);
  },
  //资讯详情
  zxbind: function (e) {
    console.log(e);
    wx.navigateTo({
      url: '/pages/zx/zx?id=' + e.currentTarget.id,
    })
  },
  //商城
  scjinr:function(){
    wx.navigateTo({
      url: '/pages/allsp/allsp',
    })
  },
  //商品详情
  spd:function(e){
    wx.navigateTo({
      url: '/pages/spdeta/spdeta?id=' + e.currentTarget.id,
    })
  },
  //初始化方法
  onLoad: function (e) {
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: 'https://wwcxc.arksh.com/api/xcx/getsha',
      data: {
        nid: 35
      },
      success: function (res) {
        console.log(res.data)
        var types = res.data.data
        for (var i = 0; i < types.length; i++) {
          var ty = types[i].type;
          if (ty == 0) {
            shar = types[i]; 
          }
        }
      }
    });
    var value = wx.getStorageSync('userInfo');
    //console.log(value);
    if (value) {
      openidXcx = value.openId
      unionid = value.unionid
    }
    deivceid = decodeURIComponent(e.scene);

    if (deivceid && deivceid !="undefined") {
      //当deivceid有值，是微信扫码进入
      console.log("微信扫码进入1");
      wx.request({
        url: 'https://wwcxc.arksh.com/api/xcx/scan', //仅为示例，并非真实的接口地址
        data: {
          unionid: unionid,
          deivceid: deivceid,
          nid: 35,
        },
        success(res) {
         // console.log("小程序扫码：" + JSON.stringify(res.data))
          var ret = res.data.ret;
          if (ret == 1) {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 5000
            })
          } else if (ret == 4) {
            console.log("小程序扫码设备未开机")
            wx.reLaunch({
              url: '/pages/pay/pay?openid=' + openidXcx + '&device=' + deivceid + "&hotel=" + res.data.hotel.company + "&devices=" + res.data.device.noid + "&hotels=" + res.data.hotel.id
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 5000
            })
            wx.request({
              url: 'https://wwcxc.arksh.com/api/xcx/indexOrder',
              data: {
                unionid: unionid
              },
              success: function (data) {
                console.log("全部订单:" + JSON.stringify(data.data));
                //console.log("全部订单:" + JSON.stringify(data.data.data[0].deviceId));

                if (data.data.data.length > 0) {
                  wx.hideLoading();
                  online = data.data.data[0].online;
                  deivceid = data.data.data[0].deviceId;
                  timer = data.data.data[0].time;
                  that.setData({
                    orderList: data.data.data,
                    js: timer,
                    deviceId: data.data.data[0].deviceId
                  })
                  that.init();
                  that.msg_callback();
                  console.log("mqtt onload");
                }
                //如果没有正在进行的订单，跳转到酒店列表
                if (data.data.data.length == 0) {
                  console.log("没有正在进行的订单，跳转到酒店列表");
                  wx.hideLoading();
                  console.log("fdfd");
                  that.setData({
                    skipurl: 'https://wwgxwx.arksh.com/wx/wwgx/geydmmq',
                  })
                }
              }
            });
          
          }
        }
      })
    }else{
      //获取用户进行中的订单
      wx.request({
        url: 'https://wwcxc.arksh.com/api/xcx/indexOrder',
        data: {
          unionid: unionid
        },
        success: function (data) {
         // console.log("全部订单:" + JSON.stringify(data.data));
          //console.log("全部订单:" + JSON.stringify(data.data.data[0].deviceId));

          if (data.data.data.length > 0) {
            wx.hideLoading();
            online = data.data.data[0].online;
            deivceid = data.data.data[0].deviceId;
            timer = data.data.data[0].time;
            that.setData({
              orderList: data.data.data,
              js: timer,
              deviceId: data.data.data[0].deviceId
            })
            that.init();
            that.msg_callback();
            //console.log("mqtt onload");
          }
          //如果没有正在进行的订单，跳转到酒店列表
          if (data.data.data.length == 0) {
            console.log("没有正在进行的订单，跳转到酒店列表");
            wx.hideLoading();
            console.log("fdfd");
            that.setData({
              skipurl: 'https://wwgxwx.arksh.com/wx/wwgx/geydmmq',
            })
          }
        }
      });
    }
    // <><><><><><> 
    //获取商品
    wx.request({
      url: 'https://mq7.arksh.com/api/user/getshp',
      data: {
        nid: 35
      },
      success: function (data) {
        //console.log(data);
        that.setData({
          splists: data.data
        })
      }
    });
      //获取资讯
      wx.request({
        url: 'https://wwcxc.arksh.com/info/blog/list',
        data: {
          nid: 35
        },
        success: function (data) {
          //console.log(data);
          that.setData({
            zxlist: data.data
          })
        }
      });
    if (!deivceid){
      deivceid = that.data.deviceId;
      }
    ///  wx.showModal({
    //   title: '加载',
    //    content: '请等待',
    // })
    var that = this;
    var value = wx.getStorageSync('userInfo')
    if (value) {
      openidXcx = value.openId
      unionid = value.unionid
    }
    //获取模式initUser:1.显示理疗、快速、记忆,0显示全部
    wx.request({
      url: 'https://wwcxc.arksh.com/api/xcx/getUserModeOrder',
      data: {
        openidXcx: openidXcx
      },
      success: function (data) {
        console.log("全部模式:" + JSON.stringify(data.data));
        console.log(openidXcx);
        var initUser = data.data.initUser
        //console.log(initUser);
        that.setData({
          initUser: true,
          allList: data.data.data
        })
        if (initUser == 0) {
          that.setData({
            initUser: true,
            allList: data.data.data
          })
        } else if (initUser == 1) {
          that.setData({
            initUser: false,
            liliaoList: data.data.data.list1,
            peedList: data.data.data.list2,
            memoryList: data.data.data.list3
          })
        }
      }
    })
    if (this.data.deviceId) {
      wx.showModal({
        title: "提示",
        content: "没有获取到设备",
        showCancel: false,
        confirmText: "确定",
        success: ''
      });
    }
  },
 
  //点击模式，弹出模式框types:1.理疗模式2.快速冲洗4.记忆模式5.遥控器模式
  pattern:function(e){
    if(online==1){
      //获取模式的类型
      console.log("选择的模式：" + JSON.stringify(e.currentTarget.dataset));
      var types = e.currentTarget.dataset.types;
      var modelName = e.currentTarget.dataset.name == undefined ? "" : e.currentTarget.dataset.name;
      var modelExplain = e.currentTarget.dataset.explain;
      var modelUrl = e.currentTarget.dataset.url;
     // console.log("types:" + types);
      //console.log("modelName:" + modelName);
      //console.log("modelExplain:" + modelExplain);
      //console.log("modelUrl:" + modelUrl);
      this.setData({
        types: types,
        modelName: modelName,
        modelExplain: modelExplain,
        modelUrl: modelUrl,
      })
      if (types == 1) {
        this.setData({
          patternLiLiao: true,
          patternPeed: false,
          patternUser: false,
          patternControl: false,
        })
        //获取理疗模式的信息：模式照片、介绍、总时间、冲洗时长
      } else if (types == 2) {
        this.setData({
          patternPeed: true,
          patternLiLiao: false,
          patternUser: false,
          patternControl: false,
        })
        //获取快速冲洗的信息：模式照片、介绍、冲洗方式、水势、水温、喷杆位置、烘干温度、冲洗时长、烘干时长
      } else if (types == 4) {
        this.setData({
          patternUser: true,
          patternLiLiao: false,
          patternPeed: false,
          patternControl: false,
        })
        //获取记忆模式的信息：模式照片、介绍、水势、水温、喷杆位置、烘干温度、冲洗时长、烘干时长
      }
      else if (types == 5) {
        this.setData({
          patternControl: true,
          patternLiLiao: false,
          patternPeed: false,
          patternUser: false,
          // modelName:'遥控器模式',
          // modelExplain:'遥控器模式',
          // modelUrl:''
        })
        //获取遥控器模式的信息：冲洗方式、水势、水温、坐温、喷杆位置、烘干温度、冲洗时长、冲洗时长
      }
      var mess = "{\'zdy\':\'1\'}";
      wx.request({
        url: 'https://wwcxc.arksh.com/api/xcx/controlOrder2', //仅为示例，并非真实的接口地址
        data: {
          deviceId: this.data.deviceId,
          nid: 1,
          mes: mess,
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          //console.log(res.data)
        }
      })
    }else{
      wx.showToast({
        title: "设备未联网",
        icon: 'none',
        duration: 1000
      })
    }
   
  },
  // 场景模式
  btnactive: function (e) {
    if(online==1){
      var val = e.currentTarget.dataset.val;
      var mess = "{\'cjkz\':\'" + val + "\'}";
      // Countdown(this);
      wx.request({
        url: 'https://wwcxc.arksh.com/api/xcx/controlOrder2', //仅为示例，并非真实的接口地址
        data: {
          deviceId: this.data.deviceId,
          nid: 1,
          mes: mess,
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
        //  console.log(res.data)
        }
      })
    }else{
      wx.showToast({
        title:"设备未联网",
        icon: 'none',
        duration: 1000
      })
    }
    
  },
  //停止真正进行的模式 1.理疗模式2.快速冲洗4.记忆模式5.遥控器模式
  stop:function(e){
   // clearTimeout(ts);
    this.setData({
      cxsj:false,
      hgsj:false
    })
    cxsjs=90;
    hgsjs=180;
    //获取模式的类型
    var types = e.currentTarget.dataset.types;
    if (types == 1) {
      this.setData({
        patternLiLiao: false
      })
    } else if (types == 2) {
      this.setData({
        patternPeed: false,
      })
    } else if (types == 4) {
      this.setData({
        patternUser: false
      })
    }
    else if (types == 5) {
      this.setData({
        patternControl: false
      })
    }
    var mess = {"cjtz":"1"};
    console.log("模式弹出框的控制>>>>mes:" + JSON.stringify(mess) + "...deviceId:" + this.data.deviceId);
    //发送关闭指令
    wx.request({
      url: 'https://wwcxc.arksh.com/api/xcx/controlOrder2', //仅为示例，并非真实的接口地址
      data: {
        deviceId: this.data.deviceId,
        nid: 1,
        mes: mess,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
       // console.log(res.data)
      }
    })
    wx.showToast({
      title: "发送成功！\n发送的内容：停止",
      icon: "none",
      mask: true
    })
  },
  //模式返回
  backtrack:function(e){
    this.setData({
      patternControl: false
    })
  },
  //显示选中的订单
  showOrder:function(e){
    var deviceId = e.currentTarget.dataset.deviceid;
    //console.log("显示选中的订单 deviceId:" + deviceId);
    this.setData({
      deviceId: deviceId,
    })
  },
  //喷头事件
  sprayHead:function(e){
    //喷头事件类型types:1.喷杆前移2.喷杆后移
    var types = e.currentTarget.dataset.types;
    if (types==1){
      if (this.data.sprayHead>=3){
        console.log("警告：已经是最大值！");
        wx.showToast({
          title: '已经是最大值！',
          icon: 'none',
          duration: 2000
        })
        return;
      }else{
        this.setData({
          sprayHead: this.data.sprayHead + 1,
        })
      }
      
    } else if (types == 2){
      if (this.data.sprayHead <= 1) {
        console.log("警告：已经是最小值！");
        wx.showToast({
          title: '已经是最大值！',
          icon: 'none',
          duration: 2000
        })
        return;
      }else{
        this.setData({
          sprayHead: this.data.sprayHead - 1,
        })
      }
     
    }
    //将喷杆指令内容编辑成字符串
   // var mess = { "cxwz": '"'+this.data.sprayHead+'"'};
    var mess = '{"cxwz":"' + this.data.sprayHead + '"}';
    console.log("模式弹出框的控制>>>>mes:" + JSON.stringify(mess) + "...deviceId:" + this.data.deviceId);
    wx.showToast({
      title: "发送成功！\n发送的内容：{喷杆:" + this.data.sprayHead + "}",
      icon: "none",
      mask: true
    })
    //发送指令
    wx.request({
      url: 'https://wwcxc.arksh.com/api/xcx/controlOrder2', //仅为示例，并非真实的接口地址
      data: {
        deviceId: this.data.deviceId,
        key: 6,
        value: this.data.sprayHead,
        nid: 1,
        mes: mess,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
      }
    })
  },
  //模式弹出框的控制,key:1.水势2.水温3.烘干风温4.清洗方式5.坐温
  controlModel:function(e){
    var key = e.currentTarget.dataset.key;
    var value = e.currentTarget.dataset.value;

    console.log("key:" + key);
    console.log("value:" + value);
//    this.setData({
    //  mesKey:this.key
   // })
   // console.log("mesKey:" + this.data.mesKey)
   // var mes = { key:value};
    var mess = '{"' + key + '":"' + value + '"}';
   // console.log("模式弹出框的控制>>>>mes:" + JSON.stringify(mes) + "...deviceId:" + this.data.deviceId);
    console.log("模式弹出框的控制>>>>mes:" + mess + "...deviceId:" + this.data.deviceId);
    wx.showToast({
      title: "发送成功！\n发送的内容：{" + key+":"+value+"}",
      icon:"none",
      mask:true
    })
    //发送指令
    wx.request({
      url: 'https://wwcxc.arksh.com/api/xcx/controlOrder2', //仅为示例，并非真实的接口地址
      data: {
        deviceId: this.data.deviceId,
        nid:1,
        mes: mess,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
      }
    })
  },
  //微光灯
  wgd:function(){
    var wgd = this.data.wgd;
    var wgs= 0;
    if(wgd==1){
        wgs = 0;
    }else{
      wgs = 1;
    }
   var mess = '{"wgd":"' + wgs + '"}';
    //发送指令
    wx.request({
      url: 'https://wwcxc.arksh.com/api/xcx/controlOrder2', //仅为示例，并非真实的接口地址
      data: {
        deviceId: this.data.deviceId,
        nid: 1,
        mes: mess,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
      }
    })
  },
  //生成记忆模式1.理疗模式2.快速冲洗4.记忆模式5.遥控器模式
  createMemory:function(e){
    console.log("生成记忆模式");
    var types = e.currentTarget.dataset.types;
    if (types==2){
      //获取快速冲洗的冲洗方式、水势、水温、喷杆位置、烘干温度、冲洗时长、烘干时长
    } else if (types == 4){
      //获取记忆模式的水势、水温、喷杆位置、烘干温度、
    } else if (types == 5) {
      //获取遥控器模式的冲洗方式、水势、水温、坐温、喷杆位置、烘干温度、冲洗时长、烘干时长
    }
    this.setData({
      showMemory: true,
      memoryType: types
    })
  },
  //生成记忆模式提交事件
  createMemory2:function(e){
    var that = this;
    console.log("openidXcx:" + openidXcx)
    //获取输入的内容
    var modelName = this.data.inputModel;
    console.log("生成记忆模名称：" + modelName);
    //获取水势
    var waterPotential = this.data.waterPotential;
    //获取水温
    var waterTemperature = this.data.waterTemperature;
    //获取烘干风温
    var dryingTemperature = this.data.dryingTemperature;
    wx.request({
      url: 'https://wwcxc.arksh.com/api/xcx/saveUserModel', //仅为示例，并非真实的接口地址
      data: {
        modelName: modelName,
        waterPotential: waterPotential,
        waterTemperature: waterTemperature,
        dryingTemperature: dryingTemperature,
        openidXcx: openidXcx,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        var ret = res.data.ret;
        console.log(openidXcx);
        if (ret==0){
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000
          }),
            //获取模式initUser:1.显示理疗、快速、记忆,0显示全部
            wx.request({
              url: 'https://wwcxc.arksh.com/api/xcx/getUserModeOrder',
              data: {
                openidXcx: openidXcx
              },
              success: function (data) {
                console.log(openidXcx);
                console.log("全部模式2:" + JSON.stringify(data.data));
                var initUser = data.data.initUser
                console.log(initUser);
                if (initUser == 0) {
                  that.setData({
                    initUser: true,
                    allList: data.data.data
                  })
                } else if (initUser == 1) {
                  that.setData({
                    initUser: false,
                    liliaoList: data.data.data.list1,
                    peedList: data.data.data.list2,
                    memoryList: data.data.data.list3
                  })
                }

              }
            })
          console.log("生成记忆模式成功！")
        }else{
          wx.showToast({
            title: '失败',
            duration: 2000
          }),
          console.log("生成记忆模式失败：" + res.data.msg)
        }
      }
    })
    //清空记忆模式input
    this.setData({
      inputModel: "",
      showMemory: false,
    });
  },
  /** 生产记忆模式的input输入框绑定事件 */
  inputModel: function (ev) {
    console.log(JSON.stringify(ev.detail.value))
    this.setData({
      inputModel: ev.detail.value
    });
  },
  //隐藏生产记忆模式弹出框
  hideMemory:function(){
    this.setData({
      showMemory: false,
    })
  },
  //紫外线杀菌提醒弹出框
  sterilizeAlert:function(e){
    var types = '';
    wx.request({
      url: 'https://wwcxc.arksh.com/api/xcx/sterilize', 
      data: {
        deviceId: this.data.deviceId,
      },
      success: function (res) {
        console.log(res.data)
        types = res.data.ret
      }
    })
    console.log("types:" + types)
    if(types==0){
      wx.showToast({
        title: '杀菌成功!!',
        icon: 'none',
        duration: 3000
      })
    } else if (types == 1) {
      wx.showToast({
        title: '已入座,不可杀菌!',
        icon: 'none',
        duration: 3000
      })
    } else if (types == 3) {
      wx.showToast({
        title: '杀菌失败!',
        icon: 'none',
        duration: 3000
      })
    } else if (types == 2) {
      console.log("types:" + types);
      wx.showModal({
        title: "紫外线杀菌提醒",
        content: "请合上马桶盖，进行杀菌",
        showCancel: false,
        confirmText: "确定",
        success: function () {
          console.log("杀菌")
        }
      });
    } 
  },
  //订单滑动事件
  bindchange:function(e){
    //console.log("订单滑动事件" + e.detail.currentItemId);
    //console.log("订单滑动事件2" + JSON.stringify(e.detail));
    var arr = (e.detail.currentItemId).split(",");
    online = arr[1];
    deivceid = arr[0];
    this.setData({
      deviceId: deivceid,
    })
    wx.setStorage({
      key: "hotelId",
      data: arr[0]
    })
  },
  //跳转上报故障页面
  fault:function(){
    console.log("跳转上报故障页面");
    wx.reLaunch({
      url: '../fault/fault'
    })
  },
  onShareAppMessage: function () {
    console.log(shar.title);
    return {
      title: shar.title,
      desc: shar.remark,
      imageUrl: shar.img
    }
  },

})
